package beans

import models.OrderM
import models.RecordM

class Order(
  val id: Int,
  val userId: Int,
  val record: Record?,
  val createTime: Long,
  val finishTime: Long?,
  val payTime: Long?,
  val state: Int,
  val toAddr: String
) {
    companion object {
        fun fromModel(orderM: OrderM, recordM: RecordM?): Order{
            val record = recordM?.let {
                Record.from(it)
            }
            return Order(
                    id = orderM.getInt(OrderM.ID),
                    userId = orderM.getInt(OrderM.USER_ID),
                    record = record,
                    state = orderM.getInt(OrderM.STATE),
                    createTime = orderM.getTimestamp(OrderM.CREATE_TIME).time,
                    finishTime = orderM.getTimestamp(OrderM.FINISH_TIME)?.time,
                    payTime = orderM.getTimestamp(OrderM.PAY_TIME)?.time,
                    toAddr = orderM[OrderM.TO_ADDR]
            )
        }

        fun formModels(models: List<OrderM>): List<Order>{
            return models.map { fromModel(it, null) }
        }
    }
}

